/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.C_hangxe;
import com.hvtt.web.models.C_hangxe;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class C_HangXeController   implements Serializable{
      private WEBConnection DAL=new WEBConnection();
      
       private C_hangxe item =new C_hangxe();
    
    public C_hangxe getItem()
    {
        return item;
    }
    public void setItem(C_hangxe item)
    {
        this.item=item;
    }
    
    public List<C_hangxe> findAllHangXe() throws SQLException
    {
        List<C_hangxe> lst= DAL.executeQuery("Select* from C_HangXe",C_hangxe.class);
        return lst;      
    }
    
     private String validateFields() throws SQLException {
        String str="";
      
        if (StringUtils.isBlank(this.getItem().getTenHang()))
        {
            str+="  + " + Utilities.getLanguages("TenHang")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
       
        if("".equals(str.trim()))
            return "";
        return "<b style='color:black;'>" + Utilities.getLanguages("AdMsg0010") + ":</b> <br/>" +  "<b style='color:red;'>" + str + "</b>";
    }
    
    
    
    public void btnSave_Click(ActionEvent event) throws IOException, SQLException {
       
        
//          this.getItem().setID(Long.valueOf(
//                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
          this.getItem().setTenHang(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TenHang")));
          
          this.getItem().setTrangThai(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TrangThai").trim()));
          
           String result = this.validateFields();
      
            if (!result.isEmpty()) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",result);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                RequestContext.getCurrentInstance().execute("update=\"growl\"");
                return;
            }
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Save");
            Params.add(this.item.getID());
            Params.add(this.item.getTenHang());
            Params.add(this.item.getTrangThai());
            DAL.executeUpdate("{call Proc_C_HangXe_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/make.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
     public void btnDelete_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Delete");
            Params.add(this.item.getID());
            Params.add("");
            Params.add(0);
            DAL.executeUpdate("{call Proc_C_HangXe_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/make.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
     public void btnNew_Click(ActionEvent event) throws IOException, SQLException {
       
        
         
          try   
          {
            this.item.resetAttribute();
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/make/new.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    public void btnUpdate_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("GetByKey");
            Params.add(this.item.getID());
            Params.add("");
            Params.add(0);
            List<C_hangxe> lst= DAL.executeQuery("{call Proc_C_HangXe_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params,C_hangxe.class);
            if(lst!=null && lst.size()>0)
            {
                this.setItem(lst.get(0));
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/make/new.html");
            }
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
}
